<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/config.inc.php');
include_once("config.php");
getConfig();
checkActiveLogin();
getUserInfo();
$cont = false;
while($cont == false){
	$abc= array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
	$receiptid = rand(11111, 99999)."-".strtoupper($abc[rand(0,25)]).rand(11,99);
	$sql = "SELECT * FROM `tbLoanReceipts`";
	$q = db::Query($sql);
	$good = true;
	while($row = $q->fetch()){
		if($row['sReceiptId'] == $receiptid){
			$good = false;
		}
	}
	if($good){
		$cont = true;
	}
}
$duedate =  strtotime($_POST['due']);
if($_POST['sType'] == ""){
	$sql = "INSERT INTO `tbLoanReceipts` (`sReceiptId`, `sFname`, `sLname`, `sBannerId`, `sEmail`, `sPhone`, `sDepartment`, `sType`, `sDate`, `sTime`, `sUname`, `sStatus`, `sDue`, `sPurpose`) VALUES (:id, :fname, :lname, :bid, :email, :phone, :department, :type, :date, :time, :uname, :status, :due, :purpose);";
	db::Query($sql, array(':id'=>$receiptid, ':fname'=>$_POST['first_name'], ':lname'=>$_POST['last_name'], ':bid'=>$_POST['banner_id'], ':email'=>$_POST['email'], ':phone'=>$_POST['phone_number'], ':department'=>$_POST['department'], ':type'=>$_POST['type'], ':date'=>getCurrentDate(), ':time'=>getCurrentTime(), ':uname'=>$USER['sUname'], ':status'=>'Awaiting Return', ':due'=>$duedate, ':purpose'=>$_POST['purpose']));
}else{
	$sql = "INSERT INTO `tbLoanReceipts` (`sReceiptId`, `sFname`, `sLname`, `sBannerId`, `sEmail`, `sPhone`, `sDepartment`, `sType`, `sDate`, `sTime`, `sUname`, `sStatus`, `sDue`, `sPurpose`) VALUES (:id, :fname, :lname, :bid, :email, :phone, :department, :type, :date, :time, :uname, :status, :due, :purpose);";
	db::Query($sql, array(':id'=>$receiptid, ':fname'=>$_POST['first_name'], ':lname'=>$_POST['last_name'], ':bid'=>$_POST['banner_id'], ':email'=>$_POST['email'], ':phone'=>$_POST['phone_number'], ':department'=>$_POST['department'], ':type'=>$_POST['type'], ':date'=>getCurrentDate(), ':time'=>getCurrentTime(), ':uname'=>$USER['sUname'], ':status'=>'On Hold', ':due'=>$duedate, ':purpose'=>$_POST['purpose']));

}
$count = 0;
foreach($_POST as $key=>$value){
	$sql = "SELECT * FROM `tbHardware` WHERE `iItemID` = :id ORDER BY `sMain` ASC";
	$q = db::Query($sql, array(':id'=>$key));
	$row = $q->fetch();
	if($row['iItemID'] != NULL){
		$count ++;
		$sql = "INSERT INTO `tbLoanLink` (`sReceiptId`, `sItem`, `sItemIdentifier`) VALUES (:id, :item, :identifier);";
		db::Query($sql, array(':id'=>$receiptid,':item'=>$value,':identifier'=>$key));
	}
}
$subject = "Software Check Out - Receipt ID: ".$receiptid;
$body = "The following software was checked out.  The submission info follows.<br/>=======================================================<br/>Requestor's Name: ".$_POST['first_name']." ".$_POST['last_name']."<br/><br/>Banner ID: ".$_POST['banner_id']."<br/><br/>E-Mail Address: ".$_POST['email']."<br/><br/>Number of Items: ".$count."<br/><br/>Referring User: ".$usr_info['sFname']." ".$usr_info['sLname']."<br/>=======================================================<br/><br/>This info has been sent to all administrators.";
sendEmail('kpeterson5@edison.edu', $subject, $body);
addActivity($USER['sFname']." ".$USER['sLname']." placed hardware on hold for ".$_POST['first_name']." ".$_POST['last_name']." - ".$receiptid);
?>
<title>Technology Software Loan Receipt</title>
<script>
var http = getHTTPObject();

function getHTTPObject(){
                var xmlhttp;
                if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
                    try {
                        xmlhttp = new XMLHttpRequest();
                    }catch(e){
                        xmlhttp = false;
                    }
                }
                return xmlhttp;
            }
			
			function php_urlencode (str) {
			str = escape(str);
			return str.replace(/[*+\/@]|%20/g,
			function (s) {
			switch (s) {
			case "*": s = "%2A"; break;
			case "+": s = "%2B"; break;
			case "/": s = "%2F"; break;
			case "@": s = "%40"; break;
			case "%20": s = "+"; break;
			}
			return s;
			}
			);
			}
			
            function loadPage(content, id){
                content = php_urlencode(content);
                http.open("GET", "Hardware_Software_Receipt_Text.php?content="+content+"&id="+id+"&rand="+Math.floor(Math.random()*99999999), true);
                http.onreadystatechange = loadPageResponse;
                http.send(null);
            }

            function loadPageResponse(){
                if(http.readyState == 4){
                    if(http.status == 404){
                    }else{
						//alert(http.responseText);
                    }
                }
            }
</script>
<body>
<div style="position:absolute; top:10; right:20; text-align:center; font-weight:bold; font-size:18px">
Receipt ID:<br/>
<?php echo $receiptid ?>
</div>
<b>
    <center>
        Edison State College<br />
        College Administrative Procedures<br />
        Division of Technology Services<br />
        <br />
        Technology Services Division Equipment Loan Program
    </center>
    <hr />
    <br>
    <br>
    <table>
        <tr>
            <td style="border-bottom:solid; border-bottom-width:1px" width="500px"><?php echo $_POST['first_name']." ".$_POST['last_name'] ?></td>
            <td width="30px">&nbsp;</td>
            <td style="border-bottom:solid; border-bottom-width:1px" width="500px"><?php echo $_POST['department'] ?></td>
        </tr>
        <tr>
            <td><b>&nbsp;&nbsp;&nbsp;Requestor's Name</b></td>
            <td width="30px">&nbsp;</td>
            <td><b>&nbsp;&nbsp;&nbsp;Department/Divison - Campus</b></td>
        </tr>
        <tr>
            <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
            <td style="border-bottom:solid; border-bottom-width:1px" width="500px"><?php echo $_POST['phone_number'] ?></td>
            <td width="30px">&nbsp;</td>
            <td style="border-bottom:solid; border-bottom-width:1px" width="500px">&nbsp;</td>
        </tr>
        <tr>
            <td><b>&nbsp;&nbsp;&nbsp;Office Phone Number</b></td>
            <td width="30px">&nbsp;</td>
            <td><b>&nbsp;&nbsp;&nbsp;Equipment Check-Out Date</b></td>
        </tr>
        <tr>
            <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
            <td style="border-bottom:solid; border-bottom-width:1px" width="500px">&nbsp;</td>
            <td width="30px">&nbsp;</td>
            <td style="border-bottom:solid; border-bottom-width:1px" width="500px"><?php echo date("F d, Y", strtotime($_POST['due'])) ?></td>
        </tr>
        <tr>
            <td><b>&nbsp;&nbsp;&nbsp;Director or VP Approval</b></td>
            <td width="30px">&nbsp;</td>
            <td><b>&nbsp;&nbsp;&nbsp;Equipment Return Date</b></td>
        </tr>
        <tr>
            <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
            <td style="border-bottom:solid; border-bottom-width:1px" colspan="3"><b>Academic/Business Purpose:</b> <?php echo $_POST['purpose'] ?></td>
        </tr>
    </table>
    <br />
    <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td><b>&nbsp;&nbsp;&nbsp;Equipment Issued</b></td>
            <td width="100px" style="border:solid; border-width:1px; padding-top:5px; padding-bottom:5px; padding-left:5px;">&#8730; = Issued</td>
            <td width="100px" style="border:solid; border-width:1px; padding-top:5px; padding-bottom:5px; padding-left:5px;">&#8730; = Returned</td>
        </tr>
            <?php
            foreach ($_POST as $key=>$value) {
				$sql = "SELECT * FROM `tbHardware` WHERE `iItemID` = :id ORDER BY `sMain` DESC";
				$q = db::Query($sql, array(':id'=>$key));
				while($row = $q->fetch()){
                    ?>
        <tr>
            <td style="border:solid; border-width:1px; padding-top:5px; padding-bottom:5px; padding-left:5px;"><?php echo $row['sDescription'] ?></td>
            <td width="100px" align="center" style="border:solid; border-width:1px;">&#8730;</td>
            <td width="100px" style="border:solid; border-width:1px;">&nbsp;</td>
        </tr>
                    <?php };
            } ?>
    </table><br />
    I have read the Technology Services Division Equipment Loan Program Procedure.  By signing below, I certify that the equipment indicated above was checked-out/checked-in as indicated and hereby agree to abide be the terms stated in the Technology Services Division Equipment Loan Program Procedure and will provide proper care according to the guidelines.  I understand that the department of the signed employee is assuming financial reponibility for the equipment.
    <table>
        <tr>
            <td><br />
                <b>CHECK-OUT:</b><br><br>
                ___________________________<br>
                Date Checked Out<br><br>
                ___________________________<br>
                <?php echo $_POST['first_name']." ".$_POST['last_name'] ?>'s Signature
            </td>

            <td width="90">
            </td>
            <td>
                <b>CHECK-IN:</b><br><br>
                ___________________________<br>
                Date Checked In<br><br>
                ___________________________<br>
                <?php echo $usr_info['sFname']." ".$usr_info['sLname'] ?> - Tech Center SA
            </td>
        </tr>
    </table>
</body>
<script>
    window.print();
	<?php if($_POST['sType'] != ""){ ?>
	alert('This item is now on hold.  Print this reciept and place it with the equipment.');
	<?php } ?>
	loadPage(document.body.innerHTML, '<?php echo $receiptid ?>');
</script>